package com.lanou.service.impl;

import com.lanou.bean.Staff;
import com.lanou.mapper.StaffMapper;
import com.lanou.service.StaffService;
import com.lanou.utils.AjaxResult;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import com.lanou.utils.SaltUtil;

import javax.annotation.Resource;

/**
 * Created by dllo on 18/4/17.
 */
@Service
public class StaffServiceImpl implements StaffService {
    @Resource
    StaffMapper staffMapper;

    @Override
    public AjaxResult changePws(@Param("oldPwd") String oldPwd,
                                @Param("newPwd") String newPwd) {
        Subject subject = SecurityUtils.getSubject();
        Staff staff = (Staff) subject.getPrincipal();
        String name = staff.getLoginName();
        staff = staffMapper.getStaff(name);
        boolean b = staff.getLoginPwd().equals(SaltUtil.setSalt(oldPwd, name));
        if (!b) {
            return AjaxResult.getNO("原密码不正确");
        } else {
            staffMapper.changePwd(SaltUtil.setSalt(newPwd, name), name);
            return AjaxResult.getOK("更改成功");
        }
    }
}
